package com.protocols.sctp.m3ua;

import java.io.Serializable;
import java.lang.reflect.Array;
import java.nio.ByteBuffer;
import java.util.Arrays;

import com.protocols.*;

public class DiagonosticInformation implements Serializable{
	private OctaveInteger ParameterTag= new OctaveInteger(7,2); // DiagonosticInfo
	private OctaveInteger ParameterLength= new OctaveInteger(4,2);	// Length=8
	private byte[] DiagonosticInfo= new byte[0];
	private Boolean Visible=false;
	private Boolean Mandatory =true;
	
	public DiagonosticInformation(Boolean mandatory) {
		Mandatory=mandatory;
	}
	
	public DiagonosticInformation(byte[] diagonosticInfo,Boolean mandatory) {		
		DiagonosticInfo=diagonosticInfo;
		setLength();
		Mandatory=mandatory;
	}

	
	public byte [] getByteArray(){		
		setLength();
		return  !Visible?new byte[0]:ProtoOperations.concatArrays(ParameterTag.getByteArray(), ParameterLength.getByteArray(), DiagonosticInfo);
		

	}
	

	public int getParameterTag() {
		return getByteArray().length;
	}


	private void setLength() {
		ParameterLength.Value = 4+ DiagonosticInfo.length;
	}
	
	public int getLength() {
		return Visible?ParameterLength.Value:0;
	}



	public byte[] getDiagonosticInfo() {
		return DiagonosticInfo;
	}


	public void setDiagonosticInfo(byte[] diagonosticInfo) {
		DiagonosticInfo = diagonosticInfo;
		setLength();
		Visible=true;
	}
	public Boolean getVisible() {
		return Visible;
	}

	public void setVisible(Boolean visible) {
		Visible = visible;
	}
	
	
	
	public byte [] decode(byte [] input) throws ProtocolStackException{
		
		if (input.length==0)
			return new byte[0];
		else if (input.length<4 && input.length>0)
			throw new ProtocolStackException("Decode Failure:Diagonostic InfoString"+ProtoOperations.getHexString(input));
		
		
		if (input[0]==0 && input[1]==7)
		{			
			ParameterLength.Value=ProtoOperations.intFromByteArray(input, 2, 2);
			setDiagonosticInfo( ProtoOperations.extractByteArray(input, 4, ParameterLength.Value-4));
			return ProtoOperations.extractByteArray(input, ParameterLength.Value, input.length-ParameterLength.Value);
		}
		else if (isMandatory())
			throw new ProtocolStackException("Decode DiagonosticInfo"+ProtoOperations.getHexString(input));
		else
			return input;
				
	}

	public Boolean isMandatory() {
		return Mandatory;
	}

	public void setMandatory(Boolean mandatory) {
		Mandatory = mandatory;
	}

	
	
	
}
